<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Loginc extends CI_Controller {

	// 默认加载后台登录界面
	public function login(){
//		//载入验证码辅助函数
//		$this->load->helper('captcha');
//		
//		$word = '';
//		$speed = 'afafdavavavafvaf';
//		for($i=0;$i<4;$i++){
//			$word .= $speed[mt_rand(0,strlen($speed)- 1)];
////			$word .= $speed[mt_rand(0, strlen($speed) - 1)];
//		}
//		
//		//配置项
//		$vals = array(
//			'word' => $word,
//			'img_path' => './captcha/',
//			'img_url' => base_url('/captcha/') ,
//			'img_width' => 100,
//			'img_height' => 30,
//			'expiration' =>1,  //过期时间，防止图片挤爆
//		);
//		//创建
//		$cap = create_captcha($vals);
////		p($cap);die;
//		$data['captcha'] = $cap['image'];
//		
//	
//		
		$this->load->view('admin/login.html');
}
	//验证码类
	/**
	 * 验证码
	 */
	public function code(){
		$config = array(
			'width'	=>	75,
			'height'=>	27,
			'codeLen'=>	1,
			'fontSize'=>16
			);
		$this->load->library('code', $config);
		//p($config);die;
		$this->code->show();

	}
	
	//登陆
	public function login_in(){
		$code = $this->input->post('captcha');
		if(!isset($_SESSION)){
			session_start();
//				session_start(); 
		}
		if(strtoupper($code) != $_SESSION['code']) error('验证码错误','admin/loginc/login');
		
		$username = $this->input->post('usename');
		$this->load->model('admin_model','login');
		$pw = $this->input->post('password');
		$userData = $this->login->check($username);	
//		p($userData);die;
		if(!$username || $userData[0]['password'] != $pw ) error('用户名或者密码错误','admin/loginc/login');
		
		$sessionData = array(
			'username' => $username,
			'uid' => $userData[0]['uid'],
 			'logintime' => time()
		);
		
		$this->session->set_userdata($sessionData);
		
		success('admin/admin/index','登陆成功');
	}
	
	//推出登陆
	public function login_out(){
		$this->session->sess_destroy();
		success('admin/loginc/login','退出成功');
		
//		$this->session->sess_destroy();
//		success('admin/login/index','退出成功');
	}
}